******* Use the file as constructed in Brazil_candidate_incumbency.do *******
clear
cd ".../Brazil_candidates_replication/"

use Brazil_candidates.dta, clear


*Descriptives
summ Ln_votes_cand ethnicity party age age2 gender education /*
*/civil camp_exp_k camp_exp_k2

*Plausbility of setting missing campex to zero
gen camp_exp_dummy=1
replace camp_exp_dummy=0 if camp_exp_k==0
pwcorr Ln_votes_cand camp_exp_dummy

*Missings
mvpatterns Ln_votes_cand ethnicity party DE1998 DF1998 PR2000 VE2000 DE2002 DF2002 /*
*/ PR2004 VE2004 DE2006 DF2006 PR2008 VE2008 DE2010 DF2010 PR2012 VE2012 SE19982012 /*
*/ GO19982012 age age2 gender education civil camp_exp_k camp_exp_k2


*** Analysis ***

*Regression for all candidates on state-level with state- and party-fixed effects
xtset state_nr
*Baseline
xtreg Ln_votes_cand ib2.ethnicity, fe
est store baseline
*Incumbency and controls without education, other socio-dems, and camp. exp
xtreg Ln_votes_cand ib2.ethnicity i.party DE1998 DF1998 PR2000 VE2000 DE2002 DF2002 /*
*/ PR2004 VE2004 DE2006 DF2006 PR2008 VE2008 DE2010 DF2010 PR2012 VE2012 SE19982012 /*
*/ GO19982012, fe
est store incumbency
*Incumbency and controls without camp. exp
xtreg Ln_votes_cand ib2.ethnicity i.party DE1998 DF1998 PR2000 VE2000 DE2002 DF2002 /*
*/ PR2004 VE2004 DE2006 DF2006 PR2008 VE2008 DE2010 DF2010 PR2012 VE2012 SE19982012 /*
*/ GO19982012 age age2 gender ib1.education i.civil, fe
est store edusocio
*Incumbency and controls with camp. exp
xtreg Ln_votes_cand ib2.ethnicity i.party DE1998 DF1998 PR2000 VE2000 DE2002 DF2002 /*
*/ PR2004 VE2004 DE2006 DF2006 PR2008 VE2008 DE2010 DF2010 PR2012 VE2012 SE19982012 /*
*/ GO19982012 age age2 gender ib1.education i.civil camp_exp_k camp_exp_k2, fe
est store full
*Table 1
estimates table baseline incumbency edusocio full, b(%7.4f) se /*
*/drop(i.party DE1998 DF1998 PR2000 VE2000 DE2002 DF2002 PR2004 VE2004 DE2006 /*
*/ DF2006 PR2008 VE2008 DE2010 DF2010 PR2012 VE2012 SE19982012 GO19982012 age /*
*/ age2 gender ib1.education i.civil camp_exp_k camp_exp_k2) stats(N r2_w) varl
*With stars
estimates table baseline incumbency edusocio full, b(%7.4f) star /*
*/drop(i.party DE1998 DF1998 PR2000 VE2000 DE2002 DF2002 PR2004 VE2004 DE2006 /*
*/ DF2006 PR2008 VE2008 DE2010 DF2010 PR2012 VE2012 SE19982012 GO19982012 age /*
*/ age2 gender ib1.education i.civil camp_exp_k camp_exp_k2) stats(N r2_w) varl
*Table Appendix
esttab full, wide b(%7.4f) se nostar l stats(N r2_w) 


***Figure 4: Coeffplot for state-level regressions
*for order of coefficients in plot
tab state_nr white_pop, nol
*3,  5,  14,  4, 10, 22, 17, 25, 27, 1,   2,  6, 16, 15, 21, 13,  8,  7,  9, 20, 11, 19, 12, 26, 18, 23, 24
*AM, BA, PA, AP, MA, RR, PI, SE, TO, AC, AL, CE, PE, PB, RO, MT, ES, DF, GO, RN, MG, RJ, MS, SP, PR, RS, SC, 

 forv i = 1/27 {
  quietly reg Ln_votes_cand cand_brown cand_black cand_asian cand_indigenous if state_nr==`i'
  estimate store state_`i'
  }
 
  forv i = 1/27 {
  quietly reg Ln_votes_cand cand_brown cand_black cand_asian cand_indigenous i.party ///
  DE1998 DF1998 PR2000 VE2000 DE2002 DF2002 PR2004 VE2004 DE2006 DF2006 PR2008 ///
  VE2008 DE2010 DF2010 PR2012 VE2012 SE19982012 GO19982012 age age2 gender ///
  ib5.education i.civil camp_exp_k camp_exp_k2 if state_nr==`i'
  estimate store state5_`i'
  }
 
 coefplot state_3 state5_3 || state_5 state5_5 || state_14 state5_14 || ///
 state_4 state5_4 || state_10 state5_10 || state_22 state5_22 || ///
 state_17 state5_17 || state_25 state5_25 || state_27 state5_27 || ///
 state_1 state5_1 || state_2 state5_2 || state_6 state5_6 || state_16 state5_16 || ///
 state_15 state5_15 || state_21 state5_21 || state_13 state5_13 || ///
 state_8 state5_8 || state_7 state5_7 || state_9 state5_9 || state_20 state5_20 || ///
 state_11 state5_11 || state_19 state5_19 || state_12 state5_12 || ///
 state_26 state5_26 || state_18 state5_18 || state_23 state5_23 || ///
 state_24 state5_24, keep(cand_brown cand_black) xline(0) ///
    bycoefs byopts(xrescale) grid(none)

***Robustness test with subsamples of competitive candidates
*Total candidate votes per state; only those with > 0 (the others are missing)
drop if votes_cand_rel<=0
bys state: summ votes_cand_rel, detail

gen above_median=0
replace above_median=1 if votes_cand_rel>.0044661 & state=="AC"
replace above_median=1 if votes_cand_rel>.0013132 & state=="AL"
replace above_median=1 if votes_cand_rel>.0008967 & state=="AM"
replace above_median=1 if votes_cand_rel>.0029424 & state=="AP"
replace above_median=1 if votes_cand_rel>.0002223 & state=="BA"
replace above_median=1 if votes_cand_rel>.0003410 & state=="CE"
replace above_median=1 if votes_cand_rel>.0011284 & state=="DF"
replace above_median=1 if votes_cand_rel>.0009680 & state=="ES"
replace above_median=1 if votes_cand_rel>.0004242 & state=="GO"
replace above_median=1 if votes_cand_rel>.0004153 & state=="MA"
replace above_median=1 if votes_cand_rel>.0001238 & state=="MG"
replace above_median=1 if votes_cand_rel>.0012911 & state=="MS"
replace above_median=1 if votes_cand_rel>.0019335 & state=="MT"
replace above_median=1 if votes_cand_rel>.0004992 & state=="PA"
replace above_median=1 if votes_cand_rel>.0009105 & state=="PB"
replace above_median=1 if votes_cand_rel>.0007116 & state=="PE"
replace above_median=1 if votes_cand_rel>.0005216 & state=="PI"
replace above_median=1 if votes_cand_rel>.0002769 & state=="PR"
replace above_median=1 if votes_cand_rel>.0001448 & state=="RJ"
replace above_median=1 if votes_cand_rel>.0013074 & state=="RN"
replace above_median=1 if votes_cand_rel>.0043405 & state=="RO"
replace above_median=1 if votes_cand_rel>.0024465 & state=="RR"
replace above_median=1 if votes_cand_rel>.0002645 & state=="RS"
replace above_median=1 if votes_cand_rel>.0008925 & state=="SC"
replace above_median=1 if votes_cand_rel>.0010064 & state=="SE"
replace above_median=1 if votes_cand_rel>.0000568 & state=="SP"
replace above_median=1 if votes_cand_rel>.0011842 & state=="TO"


keep if above_median==1

*Regression for all candidates on state-level with state- and party-fixed effects
xtset state_nr
*Baseline
xtreg Ln_votes_cand ib2.ethnicity, fe
est store baseline_med
*Incumbency and controls with camp. exp
xtreg Ln_votes_cand ib2.ethnicity i.party DE1998 DF1998 PR2000 VE2000 DE2002 DF2002 /*
*/ PR2004 VE2004 DE2006 DF2006 PR2008 VE2008 DE2010 DF2010 PR2012 VE2012 SE19982012 /*
*/ GO19982012 age age2 gender ib5.education i.civil camp_exp_k camp_exp_k2, fe
est store full_med
*Table 1
estimates table baseline_med full_med, b(%7.4f) se /*
*/drop(i.party DE1998 DF1998 PR2000 VE2000 DE2002 DF2002 PR2004 VE2004 DE2006 /*
*/ DF2006 PR2008 VE2008 DE2010 DF2010 PR2012 VE2012 SE19982012 GO19982012 age /*
*/ age2 gender ib5.education i.civil camp_exp_k camp_exp_k2) stats(N r2_w) varl
*With stars
estimates table baseline_med full_med, b star /*
*/drop(i.party DE1998 DF1998 PR2000 VE2000 DE2002 DF2002 PR2004 VE2004 DE2006 /*
*/ DF2006 PR2008 VE2008 DE2010 DF2010 PR2012 VE2012 SE19982012 GO19982012 age /*
*/ age2 gender ib5.education i.civil camp_exp_k camp_exp_k2) stats(N r2_w) varl
*Table Appendix
estimates table baseline_med full_med, b(%7.4f) se stats(N r2_w) varl
